package com.wtwd.campus.utils;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.Command;
import com.wtwd.campus.service.IRedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class InjectService {

    @Autowired
    IRedisService redisService;

    private static InjectService injectService;

    @PostConstruct
    public void init() {
        injectService = this;
    }

    //存储命令
    public static void saveCommand(String imei, String commandNumber, Object object) {
        injectService.redisService.saveCommand(imei, commandNumber, object);
    }

    //判断设备是否入库
    public static boolean judgeIfInStorage(String imei) {
        Map<String, Object> unInStorageDevice = injectService.redisService.getUnInStorageDevice();
        if (unInStorageDevice == null) {
            return true;
        }
        return unInStorageDevice.containsKey(imei) ? false : true;
    }

    //判断设备是否在线
    public static boolean deviceIsOnLine(String imei) {
        return injectService.redisService.isDeviceOnline(imei);
    }

    //刷新设备在线时间
    public static void refreshDeviceOnLineTime(String imei) {
        injectService.redisService.refreshDeviceOnlineTime(imei, 600, TimeUnit.SECONDS);
    }

    //设置设备在线
    public static void setDeviceOnline(String imei) {
        //设置设备在线状态
        injectService.redisService.setKey(Constants.DEVICE_ONLINE_PREFIX + imei, 1, 600, TimeUnit.SECONDS);
    }

    //设置考勤机设备在线
    public static void setAttendanceDeviceOnline(String imei) {
        //设置设备在线状态
        injectService.redisService.setKey(Constants.ATTENDANCE_ONLINE_PRE + imei, 1, 360, TimeUnit.SECONDS);
    }

    public static void setDeviceOnlineAndEncryptType(String imei) {
        injectService.redisService.setKey(Constants.DEVICE_ONLINE_PREFIX + imei, 2, 600, TimeUnit.SECONDS);
    }

    //移除设备在线状态
    public static void removeOnline(String imei) {
        //设置设备在线状态
        injectService.redisService.deleteKey(Constants.DEVICE_ONLINE_PREFIX + imei);
    }

    //获取设备的所有指令
    public static Map<String, Object> getCommands(String imei) {
        return injectService.redisService.getCommandsByImei(imei);
    }

    // 删除已完成的指令
    public static void delCommand(String imei, String commandNumber) {
        injectService.redisService.deleteCommand(imei, commandNumber);
    }

    // 更新指令信息
    public static void updateCommand(String imei, String commandNumber, Command command) {
        injectService.redisService.updateCommand(Constants.COMMAND_PREFIX + imei, commandNumber, command);
    }

    // 存储考勤机序列号和设备编号
    public static void saveAttendanceRepeat(String key) {
        injectService.redisService.setKey(key, 1, 1l, TimeUnit.MINUTES);
    }

    // 判断是否存在
    public static boolean judgeIfExist(String key) {
        Object value = injectService.redisService.getValue(key);
        return value == null ? false : true;
    }

    public static Integer deviceIsEncrypt(String imei) {
        Object value = injectService.redisService.getValue(Constants.DEVICE_ONLINE_PREFIX + imei);
        return (Integer) value;
    }
}
